package nccloud.framework.web.convert.translate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import nc.bs.dao.BaseDAO;
import nc.bs.logging.Logger;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.bd.cust.CustomerVO;
import nc.vo.ml.LanguageVO;
import nc.vo.ml.MultiLangContext;
import nc.vo.pub.lang.UFBoolean;
import nccloud.framework.core.collection.MapList;
import nccloud.framework.core.concurrent.AbstractBaseThread;
import nccloud.framework.core.concurrent.BaseThreadParam;
import nccloud.framework.core.env.Locator;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.model.entity.IVO;
import nccloud.framework.core.model.meta.IAttributeMeta;
import nccloud.framework.core.model.meta.IFlagItemMeta;
import nccloud.framework.core.model.meta.IFlagMeta;
import nccloud.framework.core.model.meta.IMetaResource;
import nccloud.framework.core.model.meta.IStringEnumItemMeta;
import nccloud.framework.core.model.meta.IStringEnumMeta;
import nccloud.framework.core.model.meta.IVOMeta;
import nccloud.framework.core.model.meta.JavaType;
import nccloud.framework.core.model.meta.ModelType;
import nccloud.framework.core.type.ValueUtils;
import nccloud.framework.web.convert.translate.basedoc.BaseDocIDTranslatorFactory;
import nccloud.framework.web.convert.translate.basedoc.CommonBaseDocIDTranslator;
import nccloud.framework.web.convert.translate.basedoc.IBaseDocIDTranslator;
import nccloud.framework.web.ui.config.PageTemplet;
import nccloud.framework.web.ui.model.GridModel;
import nccloud.framework.web.ui.model.row.Cell;
import nccloud.framework.web.ui.model.row.Row;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.framework.web.ui.pattern.billgrid.BillGrid;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.framework.web.ui.pattern.form.Form;
import nccloud.framework.web.ui.pattern.grid.Grid;










public class Translator
{
  class TranslateConvertorTask
    extends AbstractBaseThread<Integer>
  {
	  
	  
    private final List<TranslateCell> list;
    private final BaseThreadParam param;
    
    public TranslateConvertorTask(List<TranslateCell> list, BaseThreadParam param) {
      this.list = list;
      this.param = param;
    }

    
    public Integer run() {
      (new Translator()).translate(this.list);
      return null;
    }


    
    protected BaseThreadParam getBaseThreadParam() { return this.param; }



    
    protected boolean logSwitch() { return false; }
  }




  
  private List<String> codes = new ArrayList();



  
  private List<String> noTranslatorCodes = new ArrayList();





  
  private boolean multithread = false;





  
  private PageTemplet templet;





  
  private boolean transFlag = true;






  
  public Translator(PageTemplet templet) { this.templet = templet; }







  
  public PageTemplet getTemplet() { return this.templet; }







  
  public void setCodes(List<String> codes) { this.codes = codes; }







  
  public void setTemplet(PageTemplet templet) { this.templet = templet; }







  
  public void setTransFlag(boolean transFlag) { this.transFlag = transFlag; }






  
  public void translate(BillCard card) {
    RelationItemLoader loader = new RelationItemLoader(this.templet);
    loader.loadRelation(card);
    TranslatorBuilder builder = new TranslatorBuilder(this.templet);
    builder.setNoTranslatorCodes(this.noTranslatorCodes);
    builder.setTransflag(this.transFlag);
    builder.setCodes(this.codes);
    TranslateCell[] cells = builder.create(card);
    int rowsize = 0;
    if (card.getBody() != null && card.getBody().getModel() != null && card
      .getBody().getModel().getRows() != null) {
      rowsize = card.getBody().getModel().getRows().length;
    }
    if (rowsize >= 500000) {
      this.multithread = true;
    }
    cells = processBillCardCell(cells);
    Map<Boolean, TranslateCell[]> splitCells = splitCells(cells);
    translate((TranslateCell[])splitCells.get(Boolean.FALSE));
    noMetaTranslate((TranslateCell[])splitCells.get(Boolean.TRUE));
    resetNullRelationCellandBooleanRelationCell(card);
  }





  
  public void translate(BillGrid bgrid) {
    RelationItemLoader loader = new RelationItemLoader(this.templet);
    loader.loadRelation(bgrid);
    TranslatorBuilder builder = new TranslatorBuilder(this.templet);
    builder.setNoTranslatorCodes(this.noTranslatorCodes);
    builder.setTransflag(this.transFlag);
    builder.setCodes(this.codes);
    TranslateCell[] cells = builder.create(bgrid);
    cells = processBillGridCell(cells);
    int headrowsize = 0;
    int bodyrowsize = 0;
    int rowsize = 0;
    if (bgrid.getHead() != null && bgrid.getHead().getModel() != null && bgrid
      .getHead().getModel().getRows() != null) {
      headrowsize = bgrid.getHead().getModel().getRows().length;
    }
    if (bgrid.getBody() != null && bgrid.getBody().getModel() != null && bgrid
      .getBody().getModel().getRows() != null) {
      bodyrowsize = bgrid.getBody().getModel().getRows().length;
    }
    rowsize = headrowsize + bodyrowsize;
    Map<Boolean, TranslateCell[]> splitCells = splitCells(cells);
    if (rowsize >= 500000) {
      this.multithread = true;
    }
    translate((TranslateCell[])splitCells.get(Boolean.FALSE));
    noMetaTranslate((TranslateCell[])splitCells.get(Boolean.TRUE));
    resetNullRelationCellandBooleanRelationCell(bgrid);
  }





  
  public void translate(BillGrid[] bgrids) {
    RelationItemLoader loader = new RelationItemLoader(this.templet);
    int rowsize = 0;
    int headrowsize = 0;
    int bodyrowsize = 0;
    for (BillGrid bgrid : bgrids) {
      loader.loadRelation(bgrid);
      if (bgrid.getHead() != null && bgrid.getHead().getModel() != null && bgrid
        .getHead().getModel().getRows() != null) {
        headrowsize = bgrid.getHead().getModel().getRows().length;
      }
      if (bgrid.getBody() != null && bgrid.getBody().getModel() != null && bgrid
        .getBody().getModel().getRows() != null) {
        bodyrowsize = bgrid.getBody().getModel().getRows().length;
      }
      rowsize = rowsize + headrowsize + bodyrowsize;
    } 
    if (rowsize >= 500000) {
      this.multithread = true;
    }
    TranslatorBuilder builder = new TranslatorBuilder(this.templet);
    builder.setTransflag(this.transFlag);
    builder.setCodes(this.codes);
    builder.setNoTranslatorCodes(this.noTranslatorCodes);
    TranslateCell[] cells = builder.create(bgrids);
    cells = processBillGridCell(cells);
    Map<Boolean, TranslateCell[]> splitCells = splitCells(cells);
    translate((TranslateCell[])splitCells.get(Boolean.FALSE));
    noMetaTranslate((TranslateCell[])splitCells.get(Boolean.TRUE));
    for (BillGrid bgrid : bgrids) {
      resetNullRelationCellandBooleanRelationCell(bgrid);
    }
  }





  
  public void translate(ExtBillCard card) {
    int rowsize = 0;
    RelationItemLoader loader = new RelationItemLoader(this.templet);
    loader.loadRelation(card);
    TranslatorBuilder builder = new TranslatorBuilder(this.templet);
    builder.setTransflag(this.transFlag);
    builder.setCodes(this.codes);
    builder.setNoTranslatorCodes(this.noTranslatorCodes);
    TranslateCell[] cells = builder.create(card);
    cells = processExtBillCardCell(cells);
    Map<Boolean, TranslateCell[]> splitCells = splitCells(cells);
    for (Grid body : card.getAllBodys()) {
      if (body != null && body.getModel() != null && body.getModel().getRows() != null) {
        rowsize += body.getModel().getRows().length;
      }
    } 
    if (rowsize >= 500000) {
      this.multithread = true;
    }
    translate((TranslateCell[])splitCells.get(Boolean.FALSE));
    noMetaTranslate((TranslateCell[])splitCells.get(Boolean.TRUE));
    resetNullRelationCellandBooleanRelationCell(card);
    if("60130705p".equals(card.getPageid()) || "60130709p".equals(card.getPageid())) {
      Grid[] allgr = card.getAllBodys();
      for(Grid grid : allgr) {
      	GridModel gridmodel = grid.getModel();
      	Row[] rows = gridmodel.getRows();
      	for(Row row : rows) {
      		Map<String, Cell> map = row.getValues();
      		Cell cell = map.get("pk_jobgrade");
      		if(null != cell) {
      			String value = (String)cell.getValue();
          		String quesql ="select name from om_joblevel where dr=0 and pk_joblevel = '"+value+"'";
          		String resv="";
          		try {
          			String hvos = (String) new BaseDAO().executeQuery(quesql, new ColumnProcessor());
          			resv = hvos;
//          			if(!hvos.isEmpty()) {
//          				resv = hvos.get(0);
//          			}
          		}catch(Exception e){
          			e.printStackTrace();
          		}
          		cell.setDisplay(resv);
          		map.put("pk_jobgrade",cell);
      		}
      	}
      }
    }

  }





  
  public void translate(ExtBillCard[] cards) {
    int rowsize = 0;
    RelationItemLoader loader = new RelationItemLoader(this.templet);
    for (ExtBillCard card : cards) {
      loader.loadRelation(card);
      for (Grid body : card.getAllBodys()) {
        if (body != null && body.getModel() != null && body.getModel().getRows() != null) {
          rowsize += body.getModel().getRows().length;
        }
      } 
    } 
    
    if (rowsize >= 500000) {
      this.multithread = true;
    }
    TranslatorBuilder builder = new TranslatorBuilder(this.templet);
    builder.setTransflag(this.transFlag);
    builder.setCodes(this.codes);
    builder.setNoTranslatorCodes(this.noTranslatorCodes);
    TranslateCell[] cells = builder.create(cards);
    cells = processExtBillCardCell(cells);
    Map<Boolean, TranslateCell[]> splitCells = splitCells(cells);
    translate((TranslateCell[])splitCells.get(Boolean.FALSE));
    noMetaTranslate((TranslateCell[])splitCells.get(Boolean.TRUE));
    for (ExtBillCard card : cards) {
      resetNullRelationCellandBooleanRelationCell(card);
    }
  }





  
  public void translate(Form form) {
    RelationItemLoader loader = new RelationItemLoader(this.templet);
    loader.loadRelation(form);
    TranslatorBuilder builder = new TranslatorBuilder(this.templet);
    builder.setTransflag(this.transFlag);
    builder.setCodes(this.codes);
    builder.setNoTranslatorCodes(this.noTranslatorCodes);
    TranslateCell[] cells = builder.create(form);
    cells = processFormCell(cells);
    Map<Boolean, TranslateCell[]> splitCells = splitCells(cells);
    translate((TranslateCell[])splitCells.get(Boolean.FALSE));
    noMetaTranslate((TranslateCell[])splitCells.get(Boolean.TRUE));
    resetNullRelationCellandBooleanRelationCell(form);
  }





  
  public void translate(Grid grid) {
    int rowsize = 0;
    if (grid != null && grid.getModel() != null && grid.getModel().getRows() != null) {
      rowsize = grid.getModel().getRows().length;
    }
    if (rowsize >= 500000) {
      this.multithread = true;
    }
    RelationItemLoader loader = new RelationItemLoader(this.templet);
    if (grid != null) {
      loader.loadRelation(grid);
      TranslatorBuilder builder = new TranslatorBuilder(this.templet);
      builder.setTransflag(this.transFlag);
      builder.setNoTranslatorCodes(this.noTranslatorCodes);
      builder.setCodes(this.codes);
      TranslateCell[] cells = builder.create(grid);
      cells = processGridCell(cells);
      Map<Boolean, TranslateCell[]> splitCells = splitCells(cells);
      translate((TranslateCell[])splitCells.get(Boolean.FALSE));
      noMetaTranslate((TranslateCell[])splitCells.get(Boolean.TRUE));
      resetNullRelationCellandBooleanRelationCell(grid);
    } 
  }






  
  public void translate(Grid[] grids) {
    int rowsize = 0;
    
    RelationItemLoader loader = new RelationItemLoader(this.templet);
    for (Grid grid : grids) {
      loader.loadRelation(grid);
      if (grid != null && grid.getModel() != null && grid.getModel().getRows() != null) {
        rowsize += grid.getModel().getRows().length;
      }
    } 
    if (rowsize >= 500000) {
      this.multithread = true;
    }
    TranslatorBuilder builder = new TranslatorBuilder(this.templet);
    
    builder.setTransflag(this.transFlag);
    
    builder.setCodes(this.codes);
    builder.setNoTranslatorCodes(this.noTranslatorCodes);
    TranslateCell[] cells = builder.create(grids);
    cells = processGridCell(cells);
    
    Map<Boolean, TranslateCell[]> splitCells = splitCells(cells);
    translate((TranslateCell[])splitCells.get(Boolean.FALSE));
    noMetaTranslate((TranslateCell[])splitCells.get(Boolean.TRUE));
    for (Grid grid : grids) {
      resetNullRelationCellandBooleanRelationCell(grid);
    }
  }





  
  protected void flagTranslate(TranslateCell[] cells) {
    MapList<IVOMeta, TranslateCell> maplist = new MapList<IVOMeta, TranslateCell>();
    for (TranslateCell cell : cells) {
      if (cell.getValue() != null && !"".equals(cell.getValue().trim()))
      {
        
        if (cell.getPaths() != null) {
          IVOMeta vometa = ((MetaPath)cell.getPaths().get(0)).getCurrentVOMeta();
          maplist.put(vometa, cell);
        } 
      }
    } 
    
    translate(maplist, this.multithread);
    
    for (TranslateCell cell : cells) {
      if (cell.getValue() != null && !"".equals(cell.getValue().trim())) {

        
        cell.getCell().setValue(cell.getCell().getDisplay());
        if (cell.getPaths() != null) {
          setEnumLabel(cell);
          cell.setIsSkipReset(Boolean.TRUE);
        } 
      } 
    } 
  }




  
  protected void noMetaTranslate(TranslateCell[] cells) {
    if (cells.length == 0) {
      return;
    }
    MapList<IVOMeta, TranslateCell> maplist = new MapList<IVOMeta, TranslateCell>();
    List<TranslateCell> noMetaList = new ArrayList<TranslateCell>();
    for (TranslateCell cell : cells) {
      if (cell.getValue() != null && !"".equals(cell.getValue().trim()))
      {
        if (cell.getPaths() != null) {
          IVOMeta vometa = ((MetaPath)cell.getPaths().get(0)).getCurrentVOMeta();
          maplist.put(vometa, cell);
          if (((MetaPath)cell.getPaths().get(0)).hasNext()) {
            noMetaList.add(cell);
          }
          setEnumLabel(cell);
        } 
      }
    } 
    
    translate(maplist, this.multithread);
    
    if (noMetaList.size() > 0) {
      List<TranslateCell> flaglist = new ArrayList<TranslateCell>();
      
      for (TranslateCell cell : noMetaList) {
        ((MetaPath)cell.getPaths().get(0)).next();
        if (cell.getPaths().size() == 2) {
          ((MetaPath)cell.getPaths().get(1)).next();
        }
        IAttributeMeta current = ((MetaPath)cell.getPaths().get(0)).getCurrentAttribute();
        JavaType type = current.getJavaType();
        if (type == JavaType.UFFlag || type == JavaType.UFStringEnum) {
          flaglist.add(cell);
        }
      } 
      for (TranslateCell cell : flaglist) {
        noMetaList.remove(cell);
      }
      
      if (noMetaList.size() > 0) {
        TranslateCell[] ret = new TranslateCell[noMetaList.size()];
        ret = (TranslateCell[])noMetaList.toArray(new TranslateCell[noMetaList.size()]);
        
        this.multithread = false;
        noMetaTranslate(ret);
      } 
      
      if (flaglist.size() > 0) {
        TranslateCell[] ret = new TranslateCell[flaglist.size()];
        ret = (TranslateCell[])flaglist.toArray(ret);
        this.multithread = false;
        flagTranslate(ret);
      } 
    } 
  }







  
  protected TranslateCell[] processBillCardCell(TranslateCell[] cells) { return cells; }








  
  protected TranslateCell[] processExtBillCardCell(TranslateCell[] cells) { return cells; }








  
  protected TranslateCell[] processFormCell(TranslateCell[] cells) { return cells; }








  
  protected TranslateCell[] processGridCell(TranslateCell[] cells) { return cells; }






  
  protected void translate(TranslateCell[] cells) {
    if (cells.length == 0) {
      return;
    }
    MapList<IVOMeta, TranslateCell> maplist = new MapList<IVOMeta, TranslateCell>();
    List<TranslateCell> list = new ArrayList<TranslateCell>();
    for (TranslateCell cell : cells) {
      if (cell.getValue() != null)
      {
        if (((MetaPath)cell.getPaths().get(0)).hasNext()) {
          ((MetaPath)cell.getPaths().get(0)).next();
          if (cell.getPaths().size() == 2) {
            ((MetaPath)cell.getPaths().get(1)).next();
          }
          IVOMeta vometa = ((MetaPath)cell.getPaths().get(0)).getCurrentVOMeta();
          maplist.put(vometa, cell);
          list.add(cell);
        } else {
          setEnumLabel(cell);
        } 
      }
    } 
    translate(maplist, this.multithread);
    
    int size = list.size();
    if (size > 0) {
      TranslateCell[] ret = new TranslateCell[size];
      ret = (TranslateCell[])list.toArray(ret);
      
      this.multithread = false;
      translate(ret);
    } 
  }







  
  private TranslateCell[] processBillGridCell(TranslateCell[] cells) { return cells; }

  
  private void resetNullRelationCellandBooleanRelationCell(BillCard card) {
    if (card.getHead() != null) {
      resetNullRelationCellandBooleanRelationCell(card.getHead().getModel());
    }
    if (card.getBody() != null) {
      resetNullRelationCellandBooleanRelationCell(card.getBody().getModel());
    }
    if (card.getTail() != null) {
      resetNullRelationCellandBooleanRelationCell(card.getTail().getModel());
    }
    if (card.getGrandSons() != null) {
      for (Map.Entry<String, Grid> entry : card.getGrandSons().entrySet()) {
        if (entry.getValue() != null) {
          resetNullRelationCellandBooleanRelationCell(((Grid)entry.getValue()).getModel());
        }
      } 
    }
  }
  
  private void resetNullRelationCellandBooleanRelationCell(BillGrid card) {
    if (card.getHead() != null) {
      resetNullRelationCellandBooleanRelationCell(card.getHead().getModel());
    }
    if (card.getBody() != null) {
      resetNullRelationCellandBooleanRelationCell(card.getBody().getModel());
    }
  }
  
  private void resetNullRelationCellandBooleanRelationCell(ExtBillCard card) {
    if (card.getHead() != null) {
      resetNullRelationCellandBooleanRelationCell(card.getHead().getModel());
    }
    
    if (card.getAllBodys() != null) {
      for (Grid grid : card.getAllBodys()) {
        if (grid != null) {
          resetNullRelationCellandBooleanRelationCell(grid.getModel());
        }
      } 
    }
    if (card.getTail() != null) {
      resetNullRelationCellandBooleanRelationCell(card.getTail().getModel());
    }
  }

  
  private void resetNullRelationCellandBooleanRelationCell(Form form) { resetNullRelationCellandBooleanRelationCell(form.getModel()); }


  
  private void resetNullRelationCellandBooleanRelationCell(Grid grid) { resetNullRelationCellandBooleanRelationCell(grid.getModel()); }

  
  private void resetNullRelationCellandBooleanRelationCell(GridModel model) {
    if (model == null) {
      return;
    }
    Row[] rows = model.getRows();
    if (rows == null || rows.length == 0) {
      return;
    }
    for (Row row : rows) {
      String[] names = row.getCellNames();
      for (String name : names) {
        Cell cell = row.getCell(name);
        if (cell instanceof RelationCell) {
          if (cell.getDisplay() == null || "".equals(cell.getDisplay().trim())) {
            cell.setDisplay(null);
            cell.setValue(null);
          } else if (cell.getValue() instanceof Boolean) {
            cell.setValue(Boolean.valueOf(UFBoolean.valueOf(String.valueOf(cell.getDisplay())).booleanValue()));
          } else {
            cell.setValue(cell.getDisplay());
          } 
        }
      } 
    } 
  }


  
  private void setEnumLabel(TranslateCell cell) {
    IAttributeMeta current = ((MetaPath)cell.getPaths().get(0)).getCurrentAttribute();
    JavaType type = current.getJavaType();
    String refdoc = current.getReferenceDoc();
    if (type == JavaType.UFFlag) {
      int value = ValueUtils.getInt(cell.getValue());
      IMetaResource resource = (IMetaResource)Locator.find(IMetaResource.class);
      IFlagMeta meta = resource.getFlagMeta(refdoc);
      IFlagItemMeta item = meta.getItem(value);
      if (item != null) {
        String label = item.getLabel();
        cell.setValue(label);
      } 
    } else if (type == JavaType.UFStringEnum) {
      String value = ValueUtils.getString(cell.getValue());
      IMetaResource resource = (IMetaResource)Locator.find(IMetaResource.class);
      IStringEnumMeta flag = resource.getStringEnumMeta(refdoc);
      IStringEnumItemMeta stringEnum = flag.getItem(value);
      if (stringEnum != null) {
        String label = stringEnum.getLabel();
        cell.setValue(label);
      } 
    } 
  }







  
  private Map<Boolean, TranslateCell[]> splitCells(TranslateCell[] cells) {
    List<TranslateCell> falseList = new ArrayList<TranslateCell>();
    List<TranslateCell> trueList = new ArrayList<TranslateCell>();
    
    for (TranslateCell cell : cells) {
      if (cell.getIsNoMetaField() == Boolean.FALSE) {
        falseList.add(cell);
      } else if (cell.getIsNoMetaField() == Boolean.TRUE) {
        trueList.add(cell);
      } 
    } 
    
    Map<Boolean, TranslateCell[]> result = new HashMap<Boolean, TranslateCell[]>();
    result.put(Boolean.FALSE, falseList.toArray(new TranslateCell[falseList.size()]));
    result.put(Boolean.TRUE, trueList.toArray(new TranslateCell[trueList.size()]));
    return result;
  }







  
  private TranslateCell[] splitCells(TranslateCell[] cells, Boolean isNoMetaField) {
    List<TranslateCell> list = new ArrayList<TranslateCell>();
    for (TranslateCell cell : cells) {
      if (cell.getIsNoMetaField() == isNoMetaField) {
        list.add(cell);
      }
    } 
    return (TranslateCell[])list.toArray(new TranslateCell[list.size()]);
  }








  
  private void translate(List<TranslateCell> list) {
    Set<String> nameset = new HashSet<String>();
    Set<String> idset = new HashSet<String>();
    IVOMeta vometa = ((MetaPath)((TranslateCell)list.get(0)).getPaths().get(0)).getCurrentVOMeta();
    for (TranslateCell cell : list) {
      String id = cell.getValue();
      try {
        ModelType modType = ((MetaPath)cell.getPaths().get(0)).getCurrentAttribute().getModelType();
        String name = null;
        
        if (cell.getPaths().size() == 2) {
          modType = ((MetaPath)cell.getPaths().get(1)).getCurrentAttribute().getModelType();
          String str1 = ((MetaPath)cell.getPaths().get(0)).getCurrentAttribute().getName();
          String str2 = ((MetaPath)cell.getPaths().get(1)).getCurrentAttribute().getName();
          if (ModelType.MultiLangTextType.equals(modType)) {
            LanguageVO lvo = MultiLangContext.getInstance().getCurrentLangVO();
            if (lvo.getLangseq().intValue() == 1) {
              nameset.add(str1);
              nameset.add(str2);
            } else {
              
              nameset.add(str1);
              nameset.add(str2);
              nameset.add(str2 + lvo.getLangseq().toString());
            } 
          } else {
            nameset.add(str1);
            nameset.add(str2);
          
          }

        
        }
        else if (ModelType.MultiLangTextType.equals(modType)) {
          String str = ((MetaPath)cell.getPaths().get(0)).getCurrentAttribute().getName();
          LanguageVO lvo = MultiLangContext.getInstance().getCurrentLangVO();
          if (lvo.getLangseq().intValue() == 1) {
            name = str;
            nameset.add(name);
          } else {
            nameset.add(str);
            name = str + lvo.getLangseq().toString();
            nameset.add(name);
          } 
        } else {
          name = ((MetaPath)cell.getPaths().get(0)).getCurrentAttribute().getName();
          nameset.add(name);
        } 

        
        idset.add(id);
      } catch (Exception e) {
        Logger.error("TranslateCell cell.value:  " + id);
        Logger.error("TranslateCell cell.MetaPath.vometa entityfullname:  " + ((MetaPath)cell
            .getPaths().get(0)).getCurrentVOMeta().getEntityFullName());
        Logger.error("TranslateCell cell.MetaPath.path:  " + ((MetaPath)cell.getPaths().get(0)).getPath());
        ExceptionUtils.wrapException(e);
      } 
    } 
    
    nameset.add(vometa.getPrimaryAttribute().getName());
    int size = idset.size();
    String[] ids = new String[size];
    ids = (String[])idset.toArray(ids);
    
    size = nameset.size();
    String[] names = new String[size];
    names = (String[])nameset.toArray(names);
    
    IBaseDocIDTranslator bdtranslator = null;
    if (((TranslateCell)list.get(0)).getIsFromEditRelation().booleanValue()) {
      
      bdtranslator = CommonBaseDocIDTranslator.getInstance();
    } else {
      bdtranslator = BaseDocIDTranslatorFactory.getInstance().getTranslator(vometa);
    } 
    
    IVO[] vos = bdtranslator.queryBaseDoc(vometa, ids, names);
    bdtranslator.setValue(vos, list);
  }


  
  private void translate(MapList<IVOMeta, TranslateCell> maplist, boolean multithread) {
    if (multithread) {
      
      ExecutorService executorService = Executors.newCachedThreadPool();
      List<Future<Integer>> futureList = new ArrayList<Future<Integer>>();
      BaseThreadParam instance = BaseThreadParam.getInstance();
      Set<Map.Entry<IVOMeta, List<TranslateCell>>> set = maplist.entrySet();
      try {
        for (Map.Entry<IVOMeta, List<TranslateCell>> entry : set) {
          List<TranslateCell> list = (List)entry.getValue();
          futureList.add(executorService.submit(new TranslateConvertorTask(list, instance)));
        } 
        
        for (Future<Integer> f : futureList) {
          f.get();
        }
      } catch (Exception e) {
        ExceptionUtils.wrapException(e);
      } finally {
        executorService.shutdownNow();
      } 
    } else {
      Set<Map.Entry<IVOMeta, List<TranslateCell>>> set = maplist.entrySet();
      for (Map.Entry<IVOMeta, List<TranslateCell>> entry : set) {
        List<TranslateCell> list = (List)entry.getValue();
        translate(list);
      } 
    } 
  }



  
  public void setNoTranslatorCodes(List<String> noTranslatorCodes) { this.noTranslatorCodes = noTranslatorCodes; }
  
  public Translator() {}
}